package com.ssbc.service;


import com.ssbc.bean.RUser;
import com.ssbc.bean.Role;
import com.ssbc.bean.User;
import com.ssbc.dao.UserDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/*
    该类实现userDetailsService，返回Security包中的user对象
 */
@Service
public class UserInfoGetService implements UserDetailsService {

    @Autowired
    UserService userService;


//    public UserInfoGetService(UserDao userDao){
//        this.userDao = userDao;
//    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);
        if(user == null){
            throw new UsernameNotFoundException("用户不存在!");
        }
//        // 将权限全部放入
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        List<Role> roles = user.getRoles();
//        for(Role r: roles){
//            authorities.add(new SimpleGrantedAuthority(r.getRole_name()));
//        }
        //返回实现userDetails的user类
//        return new RUser(user);
//        return new org.springframework.security.core.userdetails.User(user.getUsername(),
//                user.getPassword(), authorities);
        UserDetails userDetails=
                org.springframework.security.core.userdetails.User.withUsername
                        (user.getUsername())
                        .password(user.getPassword())
                        .authorities("admin","user").build();
        return userDetails;
    }
}
